﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using System.Xml.Linq;
using Windows.UI.Xaml.Controls;
using BoolanSlide.RedoUndo;

namespace BoolanElement
{
    public class BooLineElement:BooLineBaseElement
    {
        Point pointX;
        public Point PointX
        {
            get { return pointX; }
            set { pointX = value; }
        }

        Point pointY;
        public Point PointY
        {
            get { return pointY; }
            set { pointY = value; }
        }

        double borderWidth;
        public double BorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; }
        }

        Color lineColor;
        public Color LineColor
        {
            get { return lineColor; }
            set { lineColor = value; }
        }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooLine line = new BooLine(this,saveForRedoUndo);
            RedoUndoHelper.SaveOriginalStatus(line);
            return line;
        }

        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooLine line = new BooLine(this);

            return line;
        }

        public static BooLineElement InitLineStyle()
        {
            BooLineElement element = new BooLineElement();
            element.ElementTransform.Rotate.Angle = 0;
            element.ElementTransform.Rotate.CenterX = 0;
            element.ElementTransform.Rotate.CenterY = 0;

            element.ElementTransform.Scale.ScaleX = 1;
            element.ElementTransform.Scale.ScaleY = 1;
            element.ElementTransform.Scale.CenterX = 0;
            element.ElementTransform.Scale.CenterY = 0;

            element.ElementTransform.Translate.X = 0;
            element.ElementTransform.Translate.Y = 0;

            element.ElementTransform.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 0);

            element.ElementTransform.Width = 200;
            element.ElementTransform.Height = 200;
            element.borderWidth = 1;
            element.lineColor = Color.FromArgb(255, 24, 122, 169);
            element.pointX.X = 0;
            element.pointX.Y = 0;
            element.pointY.X = 200;
            element.pointY.Y = 200;

            return element;
        }

        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooLineElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            XElement elementStyle = new XElement("Style");

            XElement points = new XElement("Points");
            XElement point1 = new XElement("Point1");
            point1.SetAttributeValue("X", PointX.X);
            point1.SetAttributeValue("Y", PointX.Y);
            XElement point2 = new XElement("Point2");
            point2.SetAttributeValue("X", PointY.X);
            point2.SetAttributeValue("Y", PointY.Y);
            XElement borderWidth = new XElement("BorderWidth");
            borderWidth.Value = BorderWidth.ToString();
            XElement lineColor = new XElement("LineColor");
            lineColor.Value = SerializeCast.ColorToString(LineColor);

            points.Add(point1);
            points.Add(point2);
            elementStyle.Add(points);
            elementStyle.Add(borderWidth);
            elementStyle.Add(lineColor);
            element.Add(elementStyle);

            return element;
        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement = element.Element("Transform");
            XElement stylement = element.Element("Style");

            ElementTransform.Load(tranformElement);

            XElement points = stylement.Element("Points");
            XElement point1 = points.Element("Point1");
            XElement point2 = points.Element("Point2");
            Point p1 = new Point();
            Point p2 = new Point();
            p1.X = Double.Parse(point1.Attribute("X").Value);
            p1.Y = Double.Parse(point1.Attribute("Y").Value);

            p2.X = Double.Parse(point2.Attribute("X").Value);
            p2.Y = Double.Parse(point2.Attribute("Y").Value);

            this.PointX = p1;
            this.PointY = p2;

            XElement bw = stylement.Element("BorderWidth");
            this.BorderWidth = Double.Parse(bw.Value);

            XElement linecolor = stylement.Element("LineColor");
            this.LineColor = SerializeCast.StringToColor(linecolor.Value);
        }

        public override BooElement Clone()
        {
            BooLineElement element = new BooLineElement();

            element.ElementTransform = this.ElementTransform.Clone();

            element.BorderWidth = this.BorderWidth;
            element.LineColor = Color.FromArgb(this.LineColor.A, this.LineColor.R, this.LineColor.G, this.LineColor.B);
            element.PointX = new Point(this.PointX.X, this.PointX.Y);
            element.PointY = new Point(this.PointY.X, this.PointY.Y);

            return element;
        }

        public override void Load(BooLineBaseElement shapesElement)
        {
            BooLineElement ele = shapesElement as BooLineElement;

            this.ElementTransform = ele.ElementTransform;

            this.BorderWidth = ele.BorderWidth;
            this.LineColor = Color.FromArgb(ele.LineColor.A, ele.LineColor.R, ele.LineColor.G, ele.LineColor.B);
            this.PointX = new Point(ele.PointX.X, ele.PointX.Y);
            this.PointY = new Point(ele.PointY.X, ele.PointY.Y);
        }
    }
}
